package com.ikas.ai.server.module.appraise.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.appraise.dto.req.ConstantAddReqDTO;
import com.ikas.ai.server.module.appraise.dto.req.ConstantUpdateReqDTO;
import com.ikas.ai.server.module.appraise.model.AppraiseConstant;
import com.ikas.ai.server.module.appraise.service.AppraiseConstantService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 全局常量
 *
 * @author hubowei
 * @email hu.bowei@ikasinfo.com
 * @date 2023-01-30 15:01:30
 */
@RestController
@RequestMapping("/appraise/constant")
public class AppraiseConstantController extends AbstractBean {
    @Autowired
    private AppraiseConstantService appraiseConstantService;


    /**
     * 新增全局常量和信号量
     *
     * @param req
     * @return
     */
    @PostMapping("/add")
    public String add(@RequestBody @Validated ConstantAddReqDTO req) {
        AppraiseConstant constant = new AppraiseConstant();
        BeanUtils.copyProperties(req, constant);
        constant.setCreateTime(new Date());
        constant.setCreator(getUserId());
        appraiseConstantService.save(constant);
        return JsonResult.success();
    }


    /**
     * 修改全局常量和信号量
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public String update(@RequestBody @Validated ConstantUpdateReqDTO req) {
        AppraiseConstant constant = new AppraiseConstant();
        BeanUtils.copyProperties(req, constant);
        appraiseConstantService.updateById(constant);
        return JsonResult.success();
    }

    /**
     * 通过ID删除全局常量和信号量
     *
     * @param id
     * @return
     */
    @PostMapping("/del/{id}")
    public String delete(@PathVariable("id") Long id) {
        appraiseConstantService.delete(id);
        return JsonResult.success();
    }

    /**
     * 查询所有全局常量
     *
     * @return
     */
    @GetMapping("/list")
    public String list() {
        return JsonResult.success(appraiseConstantService.list());
    }
}
